class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size();
        int target = 0;
        for (auto x : nums)target += x;
        target -= x;
        if (target < 0)return -1;
        int sum = 0, l = 0, ma = 0;
        bool f = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            while (sum > target) {
                sum -= nums[l++];
            }
            if (sum == target) {
                f = 1;
                ma = max(ma, i - l + 1);
            }
        }
        if (!f)return -1;
        return n - ma;
    }
};
